package com.jt.common.aspect;


import com.jt.common.cache.JtCacheableEvict;
import com.jt.common.cache.LruTTLCache;
import com.jt.common.cache.LruTTLCacheManger;
import com.jt.common.config.MsConfigProperties;
import com.jt.common.service.CacheService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/***
 * 清除缓存拦截器
 */
//@Order(9)
//@Aspect
//@Component
public class GoCacheEvictAspect extends BaseAspect {
	
	private LruTTLCache lruTTLCache = LruTTLCacheManger.getLruCacheInstance();

	private final static Logger logger = LoggerFactory.getLogger(GoCacheEvictAspect.class);

	//private static final String POINT_CUT = "execution(public * com..*.service.*.*(..)))";
	private static final String POINT_CUT = "@annotation(com.jt.common.cache.JtCacheableEvict)";
	@Autowired
	private CacheService cacheService;
	@Autowired
	private MsConfigProperties msConfigProperties;
	


	@Pointcut(POINT_CUT)
	public void methodPiontCut() {

	}

	/**
	 * 
	 */
	@AfterReturning("methodPiontCut()")
	public void doAfterReturning(JoinPoint joinPoint) throws Throwable{
		String targetName = joinPoint.getTarget().getClass().getName();
		String methodName = joinPoint.getSignature().getName();
		Object[] arguments = joinPoint.getArgs();
		Object result = null;
		JtCacheableEvict goCacheableEvict = getAnnotation(joinPoint,JtCacheableEvict.class);
		if(goCacheableEvict != null && msConfigProperties.isCacheEnabled()) {//开启缓存
			String key = goCacheableEvict.key();
			String condition = goCacheableEvict.condition();
			key = getCacheKey(key,condition, arguments);
			cacheService.remove(key);
			logger.debug("cache was removed, key:{}" , key);
		}
	}

}
